package com.app.twth.ui.b2cpick.category;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.databinding.ItemB2cpickCategoryListBinding;
import com.app.twth.interfaces.MyStringOnClickListener;
import com.app.twth.interfaces.MyTwoStoryOnClickListener;
import com.app.twth.ui.b2cpick.B2CPickDetails;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;


public class B2CPickCategoryAdapter extends RecyclerView.Adapter<B2CPickCategoryAdapter.MyViewHolder> {
    private int index = -1;
    private Context mContext;
    private LayoutInflater mInflater;
    private MyStringOnClickListener lpnListener;
    private MyTwoStoryOnClickListener skuListener;
    private List<B2CPickDetails> data = new ArrayList<>();

    public B2CPickCategoryAdapter(Context mContext, List<B2CPickDetails> data, MyTwoStoryOnClickListener skuListener, MyStringOnClickListener lpnListener) {
        this.mContext = mContext;
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.skuListener = skuListener;
        this.lpnListener = lpnListener;
    }

    public void setData(List<B2CPickDetails> data, int index) {
        this.data = data;
        this.index = index;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemB2cpickCategoryListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemB2cpickCategoryListBinding itemBinding;

        public MyViewHolder(ItemB2cpickCategoryListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            B2CPickDetails pick = data.get(position);
            if (Utils.isEmpty(pick.getLocationCode())) {
                itemBinding.tvItem.setText("");
            } else {
                itemBinding.tvItem.setText(pick.getLocationCode() + "");
            }
            List<B2CPickDetails.SkuDetailListDTO> pickSkuDetails = pick.getSkuDetailList();
            B2CPickCategoryDetailsAdapter loncationAdapter = new B2CPickCategoryDetailsAdapter(mContext, pickSkuDetails, skuListener, position, lpnListener);
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            itemBinding.recyclerviewLocation.setLayoutManager(layoutManager);
            itemBinding.recyclerviewLocation.setAdapter(loncationAdapter);
        }
    }

}
